package handler

import (
	"context"
	"kuangstudy-mall/srvs/goods-srv/global"
	"kuangstudy-mall/srvs/goods-srv/model"
	"kuangstudy-mall/srvs/goods-srv/proto"
)

type GoodsServiceHandler struct {
}

func (h *GoodsServiceHandler) FindGoodsList(ctx context.Context, req *proto.GoodsFilterRequest) (*proto.GoodsListResponse, error) {
	tx := global.DB.Where(&model.Goods{BaseModel: model.BaseModel{Status: 1}})
	if len(req.Keyword) > 0 {
		tx = tx.Where("name like ?", "%"+req.Keyword+"%")
	}
	if req.CategoryId > 0 {
		tx = tx.Where("category_id = ?", req.GetCategoryId())
	}

	if req.IsNew > -1 {
		tx = tx.Where("is_new = ?", req.IsNew)
	}

	if req.IsHot > -1 {
		tx = tx.Where("is_hot = ?", req.IsHot)
	}

	if req.MinPrice > 0 && req.MaxPrice > 0 && req.MaxPrice > req.MinPrice {
		tx = tx.Where("sale_price between ? and ?", req.MinPrice, req.MaxPrice)
	}

	var goodsList []model.Goods
	var total int64
	tx.Model(&model.Goods{}).Count(&total)
	tx.Scopes(Paginate(int(req.PageNo), int(req.PageSize))).Order("create_time desc").Find(&goodsList)

	var response proto.GoodsListResponse
	response.Total = total
	var goodsResponseList []*proto.GoodsResponse
	for _, goods := range goodsList {
		goodsResponseList = append(goodsResponseList, &proto.GoodsResponse{
			Id:           goods.ID,
			CreateTime:   goods.CreateAt.String(),
			UpdateTime:   goods.UpdateAt.String(),
			Name:         goods.Name,
			Sn:           goods.Sn,
			CategoryId:   goods.CategoryId,
			CategoryName: goods.CategoryName,
			OnSale:       goods.OnSale,
			IsFree:       goods.IsFree,
			IsNew:        goods.IsNew,
			IsHot:        goods.IsHot,
			Desc:         goods.Desc,
			ViewCount:    goods.ViewCount,
			BuyNum:       goods.BuyNum,
			FavNum:       goods.FavNum,
			CommentNum:   goods.CommentNum,
			IsComment:    goods.IsComment,
			SalePrice:    goods.SalePrice,
			Price:        goods.Price,
			Img:          goods.Img,
			Images:       goods.Images,
			ContentImgs:  goods.ContentImgs,
		})
	}

	response.Data = goodsResponseList
	return &response, nil
}

/*
*
根据ID查询商品信息
*/
func (h *GoodsServiceHandler) GetGoodsDetail(ctx context.Context, req *proto.GoodsInfoRequest) (*proto.GoodsResponse, error) {
	var goods model.Goods
	global.DB.Where(&model.Goods{BaseModel: model.BaseModel{ID: req.Id}}).Find(&goods)
	response := proto.GoodsResponse{
		Id:           goods.ID,
		CreateTime:   goods.CreateAt.String(),
		UpdateTime:   goods.UpdateAt.String(),
		Name:         goods.Name,
		Sn:           goods.Sn,
		CategoryId:   goods.CategoryId,
		CategoryName: goods.CategoryName,
		OnSale:       goods.OnSale,
		IsFree:       goods.IsFree,
		IsNew:        goods.IsNew,
		IsHot:        goods.IsHot,
		Desc:         goods.Desc,
		ViewCount:    goods.ViewCount,
		BuyNum:       goods.BuyNum,
		FavNum:       goods.FavNum,
		CommentNum:   goods.CommentNum,
		IsComment:    goods.IsComment,
		SalePrice:    goods.SalePrice,
		Price:        goods.Price,
		Img:          goods.Img,
		Images:       goods.Images,
		ContentImgs:  goods.ContentImgs,
	}
	return &response, nil
}

func (h *GoodsServiceHandler) BatchGetGoodsList(ctx context.Context, req *proto.BatchGoodsInfo) (*proto.GoodsListResponse, error) {
	var goodsList []model.Goods
	goodsListResponse := &proto.GoodsListResponse{}
	//result := global.DB.Where("id in ?", req.Ids).Find(&goodsList)
	result := global.DB.Where(req.Ids).Find(&goodsList)
	for _, goods := range goodsList {
		goodsListResponse.Data = append(goodsListResponse.Data, &proto.GoodsResponse{
			Id:           goods.ID,
			CreateTime:   goods.CreateAt.String(),
			UpdateTime:   goods.UpdateAt.String(),
			Name:         goods.Name,
			Sn:           goods.Sn,
			CategoryId:   goods.CategoryId,
			CategoryName: goods.CategoryName,
			OnSale:       goods.OnSale,
			IsFree:       goods.IsFree,
			IsNew:        goods.IsNew,
			IsHot:        goods.IsHot,
			Desc:         goods.Desc,
			ViewCount:    goods.ViewCount,
			BuyNum:       goods.BuyNum,
			FavNum:       goods.FavNum,
			CommentNum:   goods.CommentNum,
			IsComment:    goods.IsComment,
			SalePrice:    goods.SalePrice,
			Price:        goods.Price,
			Img:          goods.Img,
			Images:       goods.Images,
			ContentImgs:  goods.ContentImgs,
		})
	}
	goodsListResponse.Total = result.RowsAffected
	return goodsListResponse, nil
}
